package com.cp.admin.core.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cp.admin.api.utils.StrUtils;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;

/**
 * @Description ：重庆时时彩
 * @Author ： mx.li
 * @Date ： 2019/01/25 00:07
 */
public class CQSSCUtils {

    /**
     * 重庆时时彩
     * @return
     */
    public static JSONObject getCqSsc(){
        //String url = "http://api.duourl.com/api?p=json&t=cqssc&limit=1&token=d5a2491b5714b39c";

        String url = "http://vip.manycai.com/K25cabff946040a/cqssc-1.json";

        String urlAll = new StringBuffer(url).toString();
        String charset = "UTF-8";
        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
        System.out.println(jsonResult);
        if(StrUtils.isEmpty(jsonResult) || "null\r\n".equalsIgnoreCase(jsonResult)){
            return null;
        }
        JSONArray jsonArray = JSONArray.parseArray(jsonResult);
        Object object = jsonArray.get(0);// 转化为JSON类

        JSONObject expect = JSONObject.parseObject(object.toString());
        System.out.println(expect.getString("issue") + "--" + expect.getString("code") + "--" + expect.getString("opendate"));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("expect", expect.getString("issue"));
        jsonObject.put("opencode", expect.getString("code"));

        return jsonObject;
    }

    /**
     * 新疆时时彩
     * @return
     */
    public static JSONObject getXjSsc(){
        //String url = "http://api.duourl.com/api?p=json&t=xjssc&limit=1&token=d5a2491b5714b39c";

        String url = "http://vip.manycai.com/K25cabff946040a/xjssc-1.json";

        String urlAll = new StringBuffer(url).toString();
        String charset = "UTF-8";
        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
        System.out.println(jsonResult);
        if(StrUtils.isEmpty(jsonResult) || "null\r\n".equalsIgnoreCase(jsonResult)){
            return null;
        }
        JSONArray jsonArray = JSONArray.parseArray(jsonResult);
        Object object = jsonArray.get(0);// 转化为JSON类

        JSONObject expect = JSONObject.parseObject(object.toString());
        //System.out.println(expect.getString("expect") + "--" + expect.getString("opencode") + "--" + expect.getString("opentime"));

        System.out.println(expect.getString("issue") + "--" + expect.getString("code") + "--" + expect.getString("opendate"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("expect", expect.getString("issue"));
        jsonObject.put("opencode", expect.getString("code"));
        return jsonObject;
    }


    /**
     * 天津时时彩
     * @return
     */
    public static JSONObject getTjSsc(){
        String url = "http://api.duourl.com/api?p=json&t=tjssc&limit=1&token=d5a2491b5714b39c";

        String urlAll = new StringBuffer(url).toString();
        String charset = "UTF-8";
        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
        System.out.println(jsonResult);
        if(StrUtils.isEmpty(jsonResult) || "null\r\n".equalsIgnoreCase(jsonResult)){
            return null;
        }
        JSONObject object = JSON.parseObject(jsonResult);// 转化为JSON类

        JSONArray jo = object.getJSONArray("data");

        JSONObject expect = jo.getJSONObject(0);
        System.out.println(expect.getString("expect") + "--" + expect.getString("opencode") + "--" + expect.getString("opentime"));

        return expect;
    }

    /**
     * 快乐时时彩
     * @return
     */
    public static JSONObject klssc(){
        String url = "https://api.happylottery.com/data/lottery/last.xml";

        String urlAll = new StringBuffer(url).toString();
        String charset = "UTF-8";
        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
        System.out.println(jsonResult);

        return xmlToObject(jsonResult);
    }


    /**
     * 北京PK10
     * @return
     */
    public static JSONObject getBJPK10(){
        String url = "http://api.duourl.com/api?p=json&t=bjpk10&limit=1&token=d5a2491b5714b39c";

        String urlAll = new StringBuffer(url).toString();
        String charset = "UTF-8";
        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
        System.out.println(jsonResult);
        if(StrUtils.isEmpty(jsonResult) || "null\r\n".equalsIgnoreCase(jsonResult)){
            return null;
        }
        JSONObject object = JSON.parseObject(jsonResult);// 转化为JSON类

        JSONArray jo = object.getJSONArray("data");

        JSONObject expect = jo.getJSONObject(0);
        System.out.println(expect.getString("expect") + "--" + expect.getString("opencode") + "--" + expect.getString("opentime"));

        return expect;
    }


    /**
     * 快乐赛车
     * @return
     */
    public static JSONObject klsc(){
        String url = "https://api.happylottery.com/data/racing/last.xml";

        String urlAll = new StringBuffer(url).toString();
        String charset = "UTF-8";
        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
        System.out.println(jsonResult);

        return xmlToObject(jsonResult);
    }


    /**
     * 快乐飞艇
     * @return
     */
    public static JSONObject klft(){
        String url = "https://api.happylottery.com/data/airship/last.xml";

        String urlAll = new StringBuffer(url).toString();
        String charset = "UTF-8";
        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
        System.out.println(jsonResult);

        return xmlToObject(jsonResult);
    }

    public static JSONObject xmlToObject(String jsonResult){
        try {
            StringReader sr = new StringReader(jsonResult);
            InputSource is = new InputSource(sr);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(is);
            document.getDocumentElement().normalize();
            System.out.println("Root element :" + document.getDocumentElement().getNodeName());
            NodeList nList = document.getElementsByTagName("item");
            //遍历所有的书
            //for (int i = 0; i < nList.getLength(); i++) {
            Node nNode = nList.item(0);
            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            Element eElement = (Element) nNode;
            System.out.println("期数 : " + eElement.getElementsByTagName("issue").item(0).getTextContent());
            System.out.println("号码 : " + eElement.getElementsByTagName("numbers").item(0).getTextContent());
            System.out.println("时间 : " + eElement.getElementsByTagName("time").item(0).getTextContent());

            JSONObject expect = new JSONObject();
            expect.put("expect", eElement.getElementsByTagName("issue").item(0).getTextContent());
            expect.put("opencode", eElement.getElementsByTagName("numbers").item(0).getTextContent());
            expect.put("time", eElement.getElementsByTagName("time").item(0).getTextContent());
            //}
            return expect;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {

//        String url = "https://api.happylottery.com/data/racing/last.xml";
//
//        String urlAll = new StringBuffer(url).toString();
//        String charset = "UTF-8";
//        String jsonResult = HttpUrlUtils.get(urlAll, charset);// 得到JSON字符串
//        System.out.println(jsonResult);
//        try {
//            StringReader sr = new StringReader(jsonResult);
//            InputSource is = new InputSource(sr);
//            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//            DocumentBuilder builder = factory.newDocumentBuilder();
//            Document document = builder.parse(is);
//            document.getDocumentElement().normalize();
//            System.out.println("Root element :" + document.getDocumentElement().getNodeName());
//            NodeList nList = document.getElementsByTagName("item");
//            //遍历所有的书
//            //for (int i = 0; i < nList.getLength(); i++) {
//                Node nNode = nList.item(0);
//                System.out.println("\nCurrent Element :" + nNode.getNodeName());
//
//                Element eElement = (Element) nNode;
//                System.out.println("期数 : " + eElement.getElementsByTagName("issue").item(0).getTextContent());
//                System.out.println("号码 : " + eElement.getElementsByTagName("numbers").item(0).getTextContent());
//                System.out.println("时间 : " + eElement.getElementsByTagName("time").item(0).getTextContent());
//            //}
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
